package org.wz.complex.work.controller.html;

import cn.hutool.json.JSONUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import jakarta.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.wz.complex.work.entity.auth.token.TokenPclogin;
import org.wz.complex.work.service.auth.token.TokenPcloginService;

@Controller
public class HtmlController {

    private static final String VALID_PAGE = "Go821KK375mnGo";

    @Value("${server.servlet.context-path}")
    private String contextPath;

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @Resource
    private TokenPcloginService tokenService;

    @RequestMapping({"", "/"})
    public String index(Model model) {
        return "redirect:/err/10002";
    }

    @RequestMapping("/login/{token}")
    public String mid(@PathVariable(required = true) String token, Model model) {
        TokenPclogin flag = valid(token);
        if (!flag.getIsLogin()) {
            return "redirect:/err/10002";
        }
        // pre
        model.addAttribute("pre", contextPath);
        model.addAttribute("deploy", activeProfile);

        // ok
        model.addAttribute("token", token);
        model.addAttribute("tokenObject", JSONUtil.toJsonStr(flag));
        return "mid";
    }

    @RequestMapping("/html/{a}/{b}/{c}/{d}/{token}")
    public String html(
            @PathVariable(required = true) String a,
            @PathVariable(required = true) String b,
            @PathVariable(required = true) String c,
            @PathVariable(required = true) String d,
            @PathVariable(required = true) String token,
            Model model,
            @RequestParam(required = true) Long sysinfoMicroId,
            @RequestParam(required = false) String page,
            @RequestParam(required = true) String gogogocode
    ) {
        if (!VALID_PAGE.equals(gogogocode)) {
            return "redirect:/err/10002";
        }

        TokenPclogin flag = valid(token);
        if (!flag.getIsLogin()) {
            return "redirect:/err/10002";
        }
        // pre
        model.addAttribute("gogogocode", gogogocode.replaceAll("\\s+", ""));
        model.addAttribute("pre", contextPath);
        model.addAttribute("deploy", activeProfile);

        // a b c
        model.addAttribute("a", a);
        model.addAttribute("b", b);
        model.addAttribute("c", c);
        // ok
        model.addAttribute("token", token);
        model.addAttribute("tokenObject", JSONUtil.toJsonStr(flag));

        model.addAttribute("tokenComplex", flag.getTokenJwt());

        // sysinfoMicroId
        model.addAttribute("sysinfoMicroId", sysinfoMicroId);

        //
        String last = a + "/" + b + "/" + c + "/" + d;
        return  last;
    }


    private TokenPclogin valid(String token) {
        TokenPclogin one = new TokenPclogin();
        one.setIsLogin(false);
        // 校验 token
        String realToken = token.split("A")[0];
        LambdaQueryWrapper<TokenPclogin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TokenPclogin::getToken, realToken);
        one = tokenService.getOne(wrapper);
        if (null == one) {
            one = new TokenPclogin();
            one.setIsLogin(false);
        } else {
            one.setIsLogin(one.getStatus() == 1);
        }
        return one;
    }


    @RequestMapping("/err/{reason}")
    public String err(@PathVariable(required = true) String reason, Model model) {
        model.addAttribute("reason", reason);
        return "err";
    }

}
